package com.yc.fav.demo;

import org.apache.ibatis.io.ResolverUtil;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class Juit {
    public static void main(String[] args)  {
        Test1 test1 = new Test1();
        test(test1);
    }

    public static void test(Object tester)  {
        int erro = 0;
        int excep =0;
        int Ok =0;
        //获取该类定义的所以方法
        Method[] declaredMethods = tester.getClass().getDeclaredMethods();
        //所有测试方法集合
        List<Method> testMethods = new ArrayList<>();
        for (Method method : declaredMethods) {
            if (method.getAnnotation(Test.class) != null) {
                testMethods.add(method);
            }
        }
        //所有BEFORE方法集合
        List<Method> beforeMethods = new ArrayList<>();
        for (Method method : declaredMethods) {
            if (method.getAnnotation(Before.class) != null) {
                beforeMethods.add(method);
            }
        }
        //所有After方法集合
        List<Method> afterMethods = new ArrayList<>();
        for (Method method : declaredMethods) {
            if (method.getAnnotation(After.class) != null) {
                afterMethods.add(method);
            }
        }

        //执行方法集合里的方法
        for (Method method : testMethods) {
            try {
                //执行Before方法集合里的方法
                for (Method bmethod : beforeMethods) {
                    bmethod.invoke(tester);
                }
                method.invoke(tester);
                //执行方法集合里的方法
                for (Method amethod : afterMethods) {
                    amethod.invoke(tester);
                }
            }catch(InvocationTargetException ie){
                Throwable e  = ie.getCause();
             if(e instanceof AssertionError){
                erro++;
             } else if (e instanceof Exception) {
                 System.out.println(e);
                 excep++;
             }
                e.getMessage();
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } finally {
                Ok++;
            }

        }

        System.out.println("成功" + Ok + "个");
        System.out.println("错误" + erro + "个");
        System.out.println("异常" + excep + "个");

    }
}


    class Test1 {
        @Before
        public void before1() {
            System.out.println("在每个测试方法前，执行的方法Before1");

        }
        @Before
        public void before2() {
            System.out.println("在每个测试方法前，执行的方法Before2");

        }

        @After
        public  void after(){
            System.out.println("每一个方法执行后，执行的方法");
        }
        @Test
        public void test1(){
            System.out.println("测试方法1");
        }

        @Test
        public void test2(){
            System.out.println("测试方法2");
            int i=1/0;
        }
        @Test
        public void test4(){
            System.out.println("测试方法4");
        }
        @Test
        public void test3(){
            System.out.println("测试方法3");
            throw new AssertionError("断言错误");
        }

        public void test5(){
            System.out.println("不是测试方法");
        }

    }

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @interface Test {

    }
